﻿function HoursToDays(hours, hoursPerDay, how) {
    if (!hoursPerDay) {
        hoursPerDay = 24;
    }
    if (!how) {
        how = 1;
    }
    return Math.round(hours / hoursPerDay * Math.pow(10, how)) / Math.pow(10, how);
};

var DataConvertor = {};
function Project(ganttProject) {
    if (ganttProject) {
        this.Id = ganttProject.Id;
        this.Name = ganttProject.Name;
        this.ClientAction = ganttProject.ClientAction;
        this.ClientActionDate = ganttProject.ClientActionDate;
        this.ChargePersonId = ganttProject.Director.Id;
        //this.StartOn = ganttProject.StartDate;
        if (ganttProject.StartDate) this.StartOn = ganttProject.StartDate.toEpoch();
        this.ChildTasks = [];
        this.Task = [];
        this.ChangedTasks = [];
        this.TeamMembers = [];
    }
}
function Task(ganttTask) {
    if (ganttTask) {
        this.Id = ganttTask.Id;
        this.Name = ganttTask.Name;
        this.ClientAction = ganttTask.ClientAction;
        this.ClientActionDate = ganttTask.ClientActionDate;
        if (ganttTask.Director)
            this.ChargePersonId = ganttTask.Director.Id;
        //this.StartOn = ganttTask.EST;
        if (ganttTask.EST)
            this.StartOn = ganttTask.EST.toEpoch();
        this.Duration = ganttTask.Duration;
        //this.PercentCompleted = ganttTask.PercentCompleted;

        if (ganttTask.ParentTask) this.ParentTaskId = ganttTask.ParentTask.Id;
        this.MasterTaskId = ganttTask.PredecessorTaskId;
    }
}


DataConvertor.ToServerProject = function (ganttProjectInfo) {
    
    var serverProject = new Project(ganttProjectInfo);
    var i = 0;
    for (; i < ganttProjectInfo.ParentTasks.length; i++) {
        var task = DataConvertor.ToServerTask(serverProject, ganttProjectInfo.ParentTasks[i]);
        serverProject.Task[serverProject.Task.length] = task;
    }
    return serverProject;
};

DataConvertor.ToServerTask = function (project, ganttTaskInfo) {
    var serverTask = new Task(ganttTaskInfo);
    var i = 0;
    for (i = 0; i < ganttTaskInfo.ChildTasks.length; i++) {
        var subServerTask = DataConvertor.ToServerTask(project, ganttTaskInfo.ChildTasks[i]);
        project.Task[project.Task.length] = subServerTask;
    }
    return serverTask;
};



DataConvertor.ToGanttTaskInfo = function (task, ganttProjectInfo, depth) {
    var director = $(ganttProjectInfo.TeamMembers).filter(function (index, item) { return item.Id == task.ChargePersonId }).get(0);
    var ganttTaskInfo = new GanttTaskInfo(task.Id, task.Name, Date.fromEpoch(task.StartOn), task.Duration, 0, task.MasterTaskId, director, depth);
    ganttTaskInfo.Workitem = $(ganttProjectInfo.Workitem).filter(function (index, item) {
        return item.TaskId == ganttTaskInfo.Id;
    }).toArray();

    var i = 0;
    for (i = 0; i < task.ChildTasks.length; i++) {
        var subGanttTaskInfo = DataConvertor.ToGanttTaskInfo(task.ChildTasks[i], ganttProjectInfo, depth + 1);
        ganttTaskInfo.addChildTask(subGanttTaskInfo);
        ganttTaskInfo.Workitem = ganttTaskInfo.Workitem.concat(subGanttTaskInfo.Workitem);
    }
    ganttTaskInfo.Workitem = ganttTaskInfo.Workitem.sort(function (a, b) { return a.CreateOn > b.CreateOn });
    return ganttTaskInfo;
};
DataConvertor.ToGanttProjectInfo = function (project) {
    var startOn = Date.fromEpoch(project.CStartOn);
    var ganttProjectInfo = new GanttProjectInfo(project.Id, project.Name, startOn, project.Director);
    ganttProjectInfo.TeamMembers = project.TeamMembers;
    ganttProjectInfo.Workitem = project.Workitem;
    var i = 0;
    for (i = 0; i < project.RootTasks.length; i++) {
        var ganttTaskInfo = DataConvertor.ToGanttTaskInfo(project.RootTasks[i], ganttProjectInfo, 0);
        ganttTaskInfo.depth = 0;
        ganttProjectInfo.addTask(ganttTaskInfo);
    }
    ganttProjectInfo.TeamMembers = project.TeamMembers;
    return ganttProjectInfo;
};

GanttChart.prototype.loadDataIn = function (container, serverData) {
    this.initChartConfiguration();
    this.initData(serverData);
    this.create(container);
};
GanttChart.prototype.initChartConfiguration = function () {
    this.setImagePath("/content/images/gantt/");
    this.setEditable(true);
    this.showContextMenu(true);
    this.showDescTask(true, 'n,director');
    this.showDescProject(true, 'n,d,director');
};

GanttChart.prototype.initTaskTemplate = function (templateName) {
    if (templateName && templateName != "")
        this.taskTemplate = templateName;

};

GanttChart.prototype.initData = function (serverData) {
    this.taskTemplate = "taskTemplate";
    if (serverData) {
        this.addProject(DataConvertor.ToGanttProjectInfo(serverData));
        this.TeamMembers = serverData.TeamMembers;
    }

};
GanttChart.prototype.getServerData = function () {
    var serverData = {};
    var Projects = [];
    for (var i = 0; i < this.Project.length; i++) {
        Projects[i] = DataConvertor.ToServerProject(this.Project[i]);
    }

    serverData.Projects = Projects;
    return serverData;
};
GanttChart.prototype.getProject = function () {
    if (this.Project.length == 0) return null;
    return DataConvertor.ToServerProject(this.Project[0]);
};
